﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Network.Tcp;

public interface ITimer
{
    public void SetTimer(int milliSeconds)
    {
        HasSetTimer = true;
        TimeInterval = milliSeconds;
        EventCount = TimeInterval * 1000 / TimeOutMicroSecond;
        LastTimer = DateTime.Now;

        CheckTimer();
    }
    public void CheckTimer()
    {
        if (!HasSetTimer)
        {
            return;
        }
        if (++CurrentEventCount < EventCount)
        {
            return;
        }
        var milliSecond = (DateTime.Now - LastTimer).TotalMilliseconds;
        if (milliSecond > TimeInterval)
        {
            CurrentEventCount = 0;
            LastTimer = DateTime.Now;
            OnTimer();
        }
    }
    public void OnTimer();
    int TimeOutMicroSecond { get; set; }
    bool HasSetTimer { get; set; }
    int TimeInterval { get; set; }
    int EventCount { get; set; }
    int CurrentEventCount { get; set; }
    DateTime LastTimer { get; set; }
}
